<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仓库管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../../../resources/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../../../resources/css/public.css" media="all" />
</head>
<body >
<div class="layuimini-container">
<!--搜索条件开始-->
	 <div class="layuimini-main">
		<fieldset class="layui-elem-field layuimini-search">
			<legend>搜索信息</legend>
			<div style="margin: 10px 10px 10px 10px">
				<form class="layui-form layui-form-pane" lay-filter="searchFrm" id="searchFrm" name="searchFrm" method="post">
					
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">所属部门</label>
								<div class="layui-input-inline">
									<input type="text" name="deptid" id="p_deptid" class="layui-hide" />
									<input type="text" name="p_deptname" id="p_deptname" autocomplete="off" class="layui-input" readonly="readonly">
								</div>
								<div class="layui-input-inline">
									<a class="layui-btn layui-btn-warm" id="doSelectDept"><label class="layui-icon layui-icon-male"></label>选择</a>
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">用户名称</label>
								<div class="layui-input-inline">
									<input type="text" name="name" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">用户备注</label>
								<div class="layui-input-inline">
									<input type="text" name="remark" autocomplete="off" class="layui-input">
								</div>
							</div>
						</div>
						
						<div class="layui-form-item" style="text-align: center;">
							<div class="layui-inline">
								<div class="layui-inline">
									<button type="button" class="layui-btn layui-btn-normal  layui-icon layui-icon-search" lay-filter="doSearch" lay-submit="">查询</button>
									<button type="reset" class="layui-btn layui-icon layui-icon-refresh">重置</button>
								</div>
							</div>
						</div>
				</form>
			</div>
		</fieldset>
		<!--搜索条件结束-->
	
<!--搜索条件结束-->

<!--数据表格开始-->
	
		<table class="layui-hide" id="UserTable" lay-filter="UserTable"></table>
		<div id="UserToolBar" style="display: none;">
			<button type="button" lay-event="addUser" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-add-circle"></span>添加</button>
		</div>
		<div id="UserRowBar" style="display: none">
			<button type="button" lay-event="delete" class="layui-btn layui-btn-sm layui-btn-danger"><span class="layui-icon layui-icon-delete"></span>删除</button>
			<button type="button" lay-event="update" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-set-sm"></span>更新</button>
			<button type="button" lay-event="resetPwd" class="layui-btn layui-btn-sm layui-btn-warm"><span class="layui-icon layui-icon-set-sm"></span>重置密码</button>
			<button type="button" lay-event="selectRole" class="layui-btn layui-btn-sm "><span class="layui-icon layui-icon-play"></span>分配角色</button>
		</div>
		<script type="text/html" id="availableTpl">
		  <input type="checkbox" name="available" value="{{d.id}}" lay-skin="switch" lay-text="可用|不可用" lay-filter="availableSwitch" {{ d.available == 1 ? 'checked' : '' }}>
		</script>
	</div>
	
</div>
<!--数据表格结束-->

<!--添加和修改的弹出层开始-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv" >
    <form class="layui-form layui-form-pane"  lay-filter="dataFrm" id="dataFrm">
		<div class="layui-form-item">
			<label class="layui-form-label">所属部门</label>
			<div class="layui-input-inline">
				<input type="text" name="deptid" id="deptid" class="layui-hide" />
				<input type="text" id="deptid_name" readonly="readonly" name="deptname" autocomplete="off" placeholder="请选择所属部门" class="layui-input">
			</div>
			<div class="layui-input-inline">
				<a class="layui-btn layui-btn-warm" id="doDataFromSelectDept"><label class="layui-icon layui-icon-male"></label>选择</a>
			</div>
		</div>
        <div class="layui-form-item">
        	<div class="layui-inline">
        		<label class="layui-form-label">用户姓名</label>
        		<div class="layui-input-inline">
        			<input type="hidden" name="id" />
        			<input type="text" name="name" id="username" lay-verify="required" autocomplete="off" placeholder="请输入用户姓名"
        			 class="layui-input">
        		</div>
        	</div>
        	<div class="layui-inline">
        		<label class="layui-form-label">登陆名称</label>
        		<div class="layui-input-inline">
        			<input type="text" name="loginname" id="loginname" lay-verify="required" autocomplete="off" placeholder="请输入登陆名称"
        			 class="layui-input">
        		</div>
        	</div>
        </div>
        <div class="layui-form-item">
        	<label class="layui-form-label">用户地址</label>
        	<div class="layui-input-block">
        		<input type="text" name="address" autocomplete="off" placeholder="请输入用户地址" class="layui-input">
        	</div>
        </div>
        <div class="layui-form-item">
        	<label class="layui-form-label">用户备注</label>
        	<div class="layui-input-block">
        		<textarea placeholder="请输入用户备注" name="remark" class="layui-textarea"></textarea>
        	</div>
        </div>
        
        <div class="layui-form-item">
        	<div class="layui-inline">
        		<label class="layui-form-label">入职时间</label>
        		<div class="layui-input-inline">
        			<input type="text" name="hiredate" id="hiredate" lay-verify="required" readonly="readonly" autocomplete="off"
        			 placeholder="请选择入职时间" class="layui-input">
        		</div>
        	</div>
        	<div class="layui-inline">
        		<label class="layui-form-label">排序码</label>
        		<div class="layui-input-inline">
        			<input type="text" name="ordernum" id="ordernum" lay-verify="required" autocomplete="off" placeholder="请输入排序码"
        			 class="layui-input">
        		</div>
        	</div>
        </div>
        <div class="layui-form-item">
        	<div class="layui-inline">
        		<label class="layui-form-label">用户性别</label>
        		<div class="layui-input-inline">
        			<input type="radio" name="sex" value="1" title="男" checked="checked" class="layui-input">
        			<input type="radio" name="sex" value="0" title="女" class="layui-input">
        		</div>
        	</div>
        </div>
        <div class="layui-form-item" style="text-align: center;">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm layui-icon layui-icon-release" lay-filter="doSubmit" lay-submit="">提交</button>
                <button type="reset" id="dataFrmResetBtn" class="layui-btn layui-btn-warm layui-btn-sm layui-icon layui-icon-refresh" >重置</button>
            </div>
        </div>
    </form>
</div>
<!--添加和修改的弹出层结束-->

<!--分配角色弹出层开始-->
<div style="display: none;padding: 5px" id="selectUserRoleDiv">
    <table class="layui-hide" id="RoleTable" lay-filter="RoleTable"></table>
</div>
<!--分配角色弹出层结束-->

<script src="../../../../resources/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../../resources/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../../../resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../../../resources/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../../resources/common/common.js" charset="utf-8"></script>
<script src="../../../../resources/common/selectLayer.js" charset="utf-8"></script>
<script src="../../../../resources/common/Convert_Pinyin.js" charset="utf-8"></script>

<script>
    layui.use(['jquery','layer','form','table','laydate'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var layer=layui.layer;
        var table=layui.table;
		var laydate = layui.laydate;

		//渲染时间选择器
		   laydate.render({
			   elem:'#hiredate'
		   })

        var tableIndex = table.render({
            elem: '#UserTable'
            ,url: api+'user/loadAllUser'
            ,toolbar: '#UserToolBar' //开启头部工具栏，并为其绑定左侧模板
            ,title: '系统用户表'
            ,page: true
            ,cols: [ [
				 {field: 'id', title:'ID',align:'center',width:100}
                ,{field: 'name', title:'姓名',align:'center',width:100}
				,{field: 'loginname', title:'登录名',align:'center',width:100}
				,{field: 'deptname',title: '所属部门',align: "center",width:100}
                ,{field: 'hiredate', title:'入职时间',align:'center',width:200}
				,{field: 'sex',title: '性别',align: "center",width:100,templet:function(d){
                    return d.sex==1?'<font color=blue>男</font>':'<font color=red>女</font>';
                }}
				,{field: 'address', title:'用户地址',align:'center',width:100}
				,{field: 'ordernum', title:'排序码',align:'center',width:100}
				,{field: 'available',  title: '是否可用', align: "center",width: 120,templet:'#availableTpl'}
				,{field: 'remark', title:'用户备注',align:'center',width:150}
				,{field: 'imgpath', title:'用户头像',align:'center',width:150,templet:function(d){
                    return '<img width=30 height=30 src=" '+baseUrl+d.imgpath+' "/>';
                }}
                ,{fixed: 'right', title:'操作', toolbar: '#UserRowBar',align:'center',width:400}
            ] ]
            ,done:function(data,curr,count){
                //不是第一页如果返回的数据为0那么就返回上一页
                if(data.data.length==0&&curr!=1){
                    tableIndex.reload({
                        page:{
                            curr:curr-1
                        }
                    });
                }
            }
        });
		
		//监听可用操作
		 form.on('switch(availableSwitch)', function(obj){
		    $.post(api+"user/updateUser",{id:this.value,available:obj.elem.checked?1:0},function(res){
				  layer.msg(res.msg);
				  tableIndex.reload();
			 })
		   //layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
		});

        //模糊查询
        form.on('submit(doSearch)',function(data){
            var params = $("#searchFrm").serialize();
            tableIndex.reload({
                url: api+"user/loadAllUser?" + params,
                page:{
                    curr:1
                }
            })
            return false;
        });

        //头工具栏事件
        table.on('toolbar(UserTable)', function(obj){
            switch(obj.event){
                case 'addUser':
                    openAddUser();
                    break;
            };
        });


        //监听行工具条的事件
        table.on("tool(UserTable)",function(obj){
            var data = obj.data; //获得当前行数据
            switch(obj.event){
                case 'delete':
                    DeleteUser(data);
                    break;
                case 'update':
                    UpdateUser(data);
                    break;
				case 'resetPwd':
					resetPwd(data);
                case 'selectRole':
                    selectRole(data);
                    break;
            };
        });

        var url;
        var mainIndex;

        //添加添加页面
        function openAddUser() {
            mainIndex = layer.open({
                type:1,
                title:'添加用户',
                content:$("#saveOrUpdateDiv"),
                area:['800px','550px'],
                success:function(index){
                    $("#dataFrm")[0].reset();
                    url= api+"user/addUser";
					//查询最大排序码
					$.get(api + "user/queryUserMaxOrderNum", function(res) {
						$("#ordernum").val(res.data);
					})
                }
            });
        }
		
		//监听用户姓名失去焦点的事件
		$("#username").on("blur", function() {
			var name = $("#username").val();
			var fullName = pinyin.getFullChars(name);
			$("#loginname").val(fullName.toLowerCase());
		})

        //更新页面
       function UpdateUser(data){
           mainIndex = layer.open({
               type:1,
               title:'修改用户',
               content:$("#saveOrUpdateDiv"),
               area:['800px','550px'],
               success:function(index){
                   form.val("dataFrm",data);
                   url= api+"user/updateUser";
               }
           });
        }

        //保存
        form.on("submit(doSubmit)",function(obj){
            //序列化表单数据
            var params=$("#dataFrm").serialize();
            $.post(url,params,function(obj){
                layer.msg(obj.msg);
                //关闭弹出层
                layer.close(mainIndex);
                //刷新数据 表格
                if (obj.code ==200){
                    tableIndex.reload();
                }
            })
        });

     

        //删除
        function  DeleteUser(data) {
            layer.confirm('你确定要删除这条数据吗?', {icon: 3, title:'提示'}, function(index){
                $.get( api+"user/deleteUser",{id:data.id},function(res){
                    if(res.code==200){
                        tableIndex.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }
		
		//重置密码
		function resetPwd(data){
			layer.confirm('你确定要重置密码吗?', {icon: 3, title:'提示'}, function(index){
				console.log(data);
			    $.get( api+"user/resetUserPwd",{id:data.id},function(res){
			        if(res.code==200){
			            tableIndex.reload();
			        }
			        layer.msg(res.msg);
			    })
			    layer.close(index);
			});
		}
		
		//分配角色
		 function selectRole(data){
			layer.open({
				type:1,
				title:'分配角色',
				content:$("#selectUserRoleDiv"),
				area:['800px','550px'],
				btn: ['<span class=layui-icon>&#xe605;</span>确认分配'],
				yes: function(index, layero){
					var checkStatus = table.checkStatus('RoleTable');

					var id = "uid="+data.id; //用户ID

					$.each(checkStatus.data,function (index,item) {
						id += "&rids=" + item.id;
					})
					$.post( api+ "user/saveRoleUser?"+id, function (res) {
						layer.msg(res.msg);
					})
				},
				btnAlign: 'c',
				success:function(index){
					showRoleTable(data);
				}
			});
		}
		
		var roleTableIns;
		//初始化角色列表
		function showRoleTable(data) {
			roleTableIns = table.render({
				elem: '#RoleTable'
				,url: api+ 'user/initRoleByUserId'
				,where:{
					//传递参数
					id:data.id
				}
				,height: 'full-220'
				,cols: [ [
					{type: 'checkbox', fixed: 'left'}
					,{field:'id', title:'ID',align:'center'}
					,{field:'name', title:'角色名称',align:'center'}
					,{field:'remark', title:'角色备注',align:'center'}
				] ]

			})
		}
		
		//监听搜索选择的按钮事件 打开部门选择的弹出层
		$("#doSelectDept").click(function(){
			openDeptSelecter("",function(id,name){
				$("#p_deptid").val(id);
				$("#p_deptname").val(name);
			})
		})
		
		//监听添加和修改弹出层的选择部门
		$("#doDataFromSelectDept").click(function() {
			var f_id = $("#deptid").val();
			openDeptSelecter(f_id, function(id, name) {
				$("#deptid").val(id);
				$("#deptid_name").val(name);
			});
		});
    })
</script>
</body>
</html>